﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ALinq.BBS.Models;
using ALinq.BBS.DataAccess;

namespace ALinq.BBS.Controllers
{
    partial class HomeController
    {
        [ValidateInput(false)]
        public ActionResult EditTopic(int boardID, int forumID, int postListIndex,
                                      int? postID, int? replyListIndex, int? replyID)
        {
            //postID == null & replyID == null Insert Post
            //postID == null & replyID != null X
            //postID != null & replyID == null Update Post(replyID == null) or Insert reply(replyID == 0) 
            //postID != null & replyID != null Update Reply
          
            if (string.IsNullOrEmpty(base.User.Identity.Name))
            {
                return RedirectToAction("Login", new { returnUrl = Request.RawUrl });
            }

            var boards = new Boards();
            var board = boards.Single(o => o.ID == boardID);
            var forum = board.Forums.Single(o => o.ID == forumID);

            ViewBag.Board = board;
            ViewBag.Forum = forum;
            ViewBag.PostListIndex = postListIndex;
            ViewBag.ReplyListIndex = replyListIndex;

            if (postID != null && replyID == null && replyListIndex == null)
            {
                var post = forum.Posts.FindItems(o => o.ID == postID).Single();
                return View(new EditTopicModel(post));
            }

            if (postID != null && replyID != null)
            {
                var post = forum.Posts.FindItems(o => o.ID == postID).Single();
                var reply = post.Replies.Where(o => o.ID == replyID).Single();
                return View(new EditTopicModel(reply));
            }

            return View();
        }

        [ValidateInput(false)]
        [HttpPost]
        public ActionResult EditTopic(EditTopicModel topic, int boardID, int forumID, int postListIndex,
                                      int? postID, int? replyListIndex, int? replyID)
        {
            var userName = base.User.Identity.Name;
            if (string.IsNullOrEmpty(userName))
            {
                return RedirectToAction("Login", new { returnUrl = Request.RawUrl });
            }
            var boards = new Boards();
            var board = boards.Single(o => o.ID == boardID);
            var forum = board.Forums.Single(o => o.ID == forumID);

            ViewBag.Board = board;
            ViewBag.Forum = forum;
            ViewBag.PostListIndex = postListIndex;
            ViewBag.ReplyListIndex = replyListIndex;

            if (ModelState.IsValid)
            {
                Post post = null;
                if (postID != null)
                {
                    post = forum.Posts.Single(o => o.ID == postID);
                }

                //TODO:检查权限
                if (post != null)
                {
                    if (replyListIndex == null)
                    {
                        forum.UpdatePost(postID.Value, topic.Title, topic.Content, userName);
                    }
                    else
                    {
                        if (replyID == 0)
                            post.Replies.AddItem(topic.Title, topic.Content, userName);
                        else
                            post.Replies.UpdateItem(postID.Value, topic.Title, topic.Content, userName);
                    }
                }
                else
                {
                    forum.AddPost(topic.Title, topic.Content, userName);
                }


                if (postID != null)
                {
                    //TODO:转至指定的回复
                    var obj = new { boardID, forumID, postListIndex, postID, replyListIndex };
                    return RedirectToRoute(RouteNames.Post, obj);
                }
                return RedirectToRoute(RouteNames.PostList, new { boardID, forumID, postListIndex });
            }
            return View(topic);
        }

    }
}